#!/bin/bash
#description: install gitlab server on centos7.4
ver=$1
domain="git.51yuki.cn"
email="testgit@sohu.com"
passwd="As@123321"
datadir="/data/git"

if [ $# -ne 1 ];then
	echo $"Usage: $0 <version>"
	exit 1
fi
#安装依赖包
yum -y install curl policycoreutils-python openssh-server openssh-clients postfix

#启动sshd和postfix
systemctl enable sshd
systemctl start sshd

systemctl enable postfix
systemctl start postfix

#firewalld防火墙配置
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
systemctl reload firewalld

#安装gitlab server

cd /usr/local/src
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-${ver}-ce.0.el7.x86_64.rpm
rpm -ivh gitlab-ce-${ver}-ce.0.el7.x86_64.rpm
#配置访问域名

sed -i "s@external_url 'http://localhost'@external_url 'http://${domain}'@" /etc/gitlab/gitlab.rb

#配置邮箱
cat >>/etc/gitlab/gitlab.rb <<EOF
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = '$email'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.$(echo $email |awk -F @ '{print $2}')"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "$email"
gitlab_rails['smtp_password'] = "$passwd"
gitlab_rails['smtp_domain'] = "$(echo $email |awk -F @ '{print $2}')"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = false
gitlab_rails['smtp_tls'] = false
user['git_user_email']="$email"
EOF

#配置gitlab的仓库目录
[ ! -d $datadir ] && mkdir -p $datadir
echo 'git_data_dir "$datadir"'>>/etc/gitlab/gitlab.rb

#重新配置，让其生效
gitlab-ctl reconfigure

